﻿//====================================================================================================================
//Copyright (c) 2011 IdeaBlade
//====================================================================================================================
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//====================================================================================================================
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of 
//the Software.
//====================================================================================================================
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
//OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
//====================================================================================================================

using System.Collections.Generic;
using System.Linq;
using HelloWorld.Repository;
using HelloWorld.Tests.Fakes;
using HelloWorld.ViewModel;
using IdeaBlade.Application.Framework.Core.Persistence;
using IdeaBlade.EntityModel;
using IdeaBlade.TestFramework;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HelloWorld.Tests
{
    [TestClass]
    public class OrderScreenViewModelTests : DevForceTest
    {
        [TestMethod]
        [Tag("Sync")]
        public void ShouldGetCustomers()
        {
            var vm = new OrderScreenViewModel(new FakeOrderManagementRepository(), null, null);

            vm.Start();

            Assert.IsTrue(vm.Customers.Count == 10, "Should have retrieved 10 customers");
        }

        [TestMethod]
        [Tag("Sync")]
        public void ShouldGetCustomersFromRepositorySynchronous()
        {
            var repository =
                new OrderManagementRepository(
                    new DesignTimeEntityManagerProvider(
                        new SampleDataProvider(new SampleDataLoader())));
            repository.GetCustomers(customers => Assert.IsTrue(customers.Any(), "Should have retrieve customers"));
        }

        public INotifyCompleted ResetFakeBackingStore(IEntityManagerProvider<NorthwindIBEntities> entityManagerProvider)
        {
            var provider = entityManagerProvider as BaseFakeStoreEntityManagerProvider<NorthwindIBEntities>;
            if (provider != null)
                return provider.ResetFakeBackingStoreAsync();

            return AlwaysCompleted.Instance;
        }

        public INotifyCompleted TestInitAsync(IEntityManagerProvider<NorthwindIBEntities> entityManagerProvider)
        {
            var commands = new List<CoroutineCommand>
                               {
                                   new CoroutineCommand(entityManagerProvider.InitializeAsync),
                                   new CoroutineCommand(() => ResetFakeBackingStore(entityManagerProvider))
                               };
            return commands.Execute();
        }

        [TestMethod]
        [Asynchronous, Timeout(10000)]
        [Tag("Async")]
        public void ShouldGetCustomersFromRepositoryAsynchronous()
        {
            DoItAsync(
                () =>
                {
                    var customers = new List<Customer>();

                    var entityManagerProvider =
                        new DevelopmentEntityManagerProvider(
                            new SampleDataProvider(new SampleDataLoader()));
                    var repository = new OrderManagementRepository(entityManagerProvider);

                    var commands = new List<CoroutineCommand>
                                           {
                                               new CoroutineCommand(() => TestInitAsync(entityManagerProvider)),
                                               new CoroutineCommand(() => repository.GetCustomers(customers.AddRange))
                                           };

                    CoroutineOperation op = commands.Execute();
                    op.Completed += (s, args) =>
                                        {
                                            Assert.IsTrue(customers.Count > 0,
                                                          "Should have retrieved customers");
                                            TestComplete();
                                        };
                }
                );
        }
    }
}